<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/11/27
 * Time: 18:00
 */
namespace app\common\helper;

class user_helper
{
    /**
     * 获取当前登录的用户id
     * @return int
     */
    public static function get_user_id()
    {
        static $static_get_user_id = NULL;
        if ($static_get_user_id === NULL) {
            if (isset($_SESSION['user_id'])) {
                $static_get_user_id = $_SESSION['user_id'];
            } else {
                $static_get_user_id = 0;
            }
        }

        if ($static_get_user_id == 0 && !is_weixin()) {
            // if ($static_get_user_id == 0 && ONDEV) {
            $static_get_user_id = 100029;
        }

        return $static_get_user_id;
    }

    /**
     * 获取open_id
     * @param number $user_id
     * @return open_id
     */
    public static function get_open_id($user_id = 0)
    {
        if (!$user_id) {
            $user_id = self::get_user_id();
        }
        if (!$user_id) {
            $user_id = $_SESSION['wx_id'];
        }
        if (!$user_id) {
            return 0;
        }

        return _uri('user', ['id' => $user_id], 'open_id');
    }
    /**
     * 根据filter取用户数量
     * @param array $filter
     * @return int
     */
    public static function get_user_num_by_filter($filter)
    {
        if (!$filter) {
            return 0;
        }

        return getTotal('user', $filter);
    }

    /**
     * 取得用户的信息，如果不传$user_id，刚取当前登陆用户
     * @param int $user_id
     * @return array
     */
    public static function get_user_info($user_id)
    {
        if (!$user_id) {
            return array();
        }

        return _uri('user', ['id'=>$user_id]);
    }

    /**
     * 获得当前用户的企业id, 或者通过用户id取得company_id
     * @param int $user_id 用户id
     * @return int
     */
    public static function get_user_company_id($user_id = 0)
    {
        if (!$user_id) {
            $user_id = self::get_user_id();
        }

        return _uri('user', ['id'=>$user_id], 'company_id');
    }

}